<?php
include_once 'config.php';
include_once  'addstock_functions.php';
include_once  'upload-csv_functions.php';
$username="tungt";
if ($_FILES["file"]["error"] > 0)
{
	echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
	$data  = read_csv($_FILES["file"]["tmp_name"]);
	$index = 0;
	foreach ($data as $r) {
		if($index == 0){
			$index ++;
		}else{
			$stock = $r[0];
			$type ="";
			$value = 0;
			if(empty($r[1])|| is_null($r[1]) || is_nan($r[1])){
				$type = 'Sell';
				$value  = intval(str_replace(',',"",$r[2]));
			}else{
				$type = 'Buy';
				$value  = intval(str_replace(',',"",$r[1]));
			}
			$transactionDate = $r[3];
			$quality = intval(str_replace(',',"",$r[4]));
			save($DB_NAME,$username,$stock,$type,$value,$quality,$transactionDate);
		}
	}
}
header("location:upload-csv.php");
?>